﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.IO;
using System.Windows.Forms;
using prDe_Tai.trunggian;
namespace prDe_Tai
{
    public partial class CapNhat : Form
    {
        public CapNhat()
        {
            InitializeComponent();
        }
        string loaiphanmem;
        string chude;
        trunggian_danhsachphanmem obj = new trunggian_danhsachphanmem();
        DataSet ds;
        BindingSource bs = new BindingSource();
        bool danapdulieuvaocombobox = false;
        void loadtenphanmemlencombobox()
        {
            ds = obj.laytoanbophanmem();
            danapdulieuvaocombobox = false;
            comboBox1.DataSource = ds.Tables[0];
            comboBox1.DisplayMember = "tenphanmem";
            comboBox1.ValueMember = "tenphanmem";
            danapdulieuvaocombobox = true;
        }

        void hienthidulieulenradiobutton()
        {
            if (loaiphanmem == "miễn phí")
                radMienPhi.Checked = true;
            else
                radCoPhi.Checked = true;
            if (chude == "System")
                radSystem.Checked = true;
            else if (chude == "Virus-Security")
                radVirus_Security.Checked = true;
            else if (chude == "Graphics")
                radGraphics.Checked = true;
            else if (chude == "Multimedias")
                radMultimedias.Checked = true;
            else if (chude == "Style-Themes")
                radStyles_Themes.Checked = true;
            else
                radUtilities.Checked = true;
        }

        public void capnhatphanmem()
        {
            DataRowView drv = (DataRowView)bs.Current;
            drv["tenphanmem"] = comboBox1.SelectedValue.ToString();
            bs.EndEdit();
            obj.capnhatvaodatabase(ds.Tables[0]);
            mo_dong_caccontrol(false);
            comboBox1.Enabled = true;
            btnChinhSua.Enabled = true;
            dacapnhat = true;
        }

        private void CapNhat_Load(object sender, EventArgs e)
        {
            if (frmMainForm.capnhat_tenphanmem=="")
            {
                loadtenphanmemlencombobox();                
            }
            else if (frmMainForm.capnhat_tenphanmem!= "")
            {
                loadtenphanmemlencombobox();
                comboBox1.SelectedValue = frmMainForm.capnhat_tenphanmem;                
            }
            else
            {
                loadtenphanmemlencombobox();                
            }
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (danapdulieuvaocombobox)
            {
                btnChinhSua.Enabled = true;
                ds = obj.layphanmemtheotenphanmem(comboBox1.SelectedValue.ToString());
                bs.DataSource = ds.Tables[0];
                txtChucNang.DataBindings.Clear();
                txtDiaChiDownload.DataBindings.Clear();
                groupBox2.DataBindings.Clear();
                groupBox1.DataBindings.Clear();
                txtHinh.DataBindings.Clear();            
                txtChucNang.DataBindings.Add("text", bs, "chucnang");
                txtDiaChiDownload.DataBindings.Add("text", bs, "diachidownload");
                groupBox1.DataBindings.Add("tag", bs, "loaiphanmem");
                groupBox2.DataBindings.Add("tag", bs, "chude");
                txtHinh.DataBindings.Add("text", bs, "hinh");
                loaiphanmem = groupBox1.Tag.ToString(); ;
                chude = groupBox2.Tag.ToString();
                label3.Text = groupBox1.Tag.ToString();
                label5.Text = groupBox2.Tag.ToString();
                hienthidulieulenradiobutton();               
            }
        }
       
        private void btnThemChuongTrinh_Click(object sender, EventArgs e)
        {
            try
            {
                if (txtHinh.Text != "")
                {
                    FileInfo myfile = new FileInfo(txtHinh.Text);
                    if (txtHinh.Text.Substring(txtHinh.Text.Length - 4) != ".jpg" || myfile.Exists == false)
                    {
                        MessageBox.Show("Phần mở rộng của hình không phải là \"jpg\" hoặc địa chỉ hình không tồn tại...", "Looking for software", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        txtHinh.Focus();
                        txtHinh.Clear();
                    }
                    else
                        capnhatphanmem();
                }
                else
                    capnhatphanmem();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Looking For Software", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }      
        private void radMienPhi_CheckedChanged(object sender, EventArgs e)
        {
            if (radMienPhi.Checked == true)
                groupBox1.Tag = radMienPhi.Tag;
            else if (radCoPhi.Checked == true)
                groupBox1.Tag = radCoPhi.Tag;           
        }
        private void radSystem_CheckedChanged(object sender, EventArgs e)
        {
            if (radSystem.Checked == true)
                groupBox2.Tag = radSystem.Text;
            else if (radVirus_Security.Checked == true)
                groupBox2.Tag = radVirus_Security.Text;
            else if (radGraphics.Checked == true)
                groupBox2.Tag = radGraphics.Text;
            else if (radMultimedias.Checked == true)
                groupBox2.Tag = radMultimedias.Text;
            else if (radStyles_Themes.Checked == true)
                groupBox2.Tag = radStyles_Themes.Text;
            else if (radUtilities.Checked == true)
                groupBox2.Tag = radUtilities.Text;
        }
        public void mo_dong_caccontrol(bool bien)
        {
            groupBox1.Enabled = bien;
            groupBox2.Enabled = bien;
            btnThemChuongTrinh.Enabled = bien;
            txtChucNang.Enabled = bien;
            txtDiaChiDownload.Enabled = bien;
            txtHinh.Enabled = bien;
            button1.Enabled = bien;
        }
        private void btnChinhSua_Click(object sender, EventArgs e)
        {           
                comboBox1.Enabled = false;
                mo_dong_caccontrol(true);
                btnChinhSua.Enabled = false;            
        }  
        private void button1_Click(object sender, EventArgs e)
        {
            OpenFileDialog myfile = new OpenFileDialog();
            if (myfile.ShowDialog() == DialogResult.OK)
            {
                string chuoi = myfile.FileName.Substring(myfile.FileName.Length - 4);
                if (chuoi != ".jpg")
                {
                    MessageBox.Show("Chương trình chỉ nạp hình có phần mở rộng *.jpg", "Looking for software", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    button1_Click(sender, e);
                }
                else
                {
                    txtHinh.Text = myfile.FileName;
                    frmMainForm.vitrihinhdathaydoi = false;
                }
            }
        }
        public bool dacapnhat = false;
        private void CapNhat_FormClosing(object sender, FormClosingEventArgs e)
        {
            if(dacapnhat==true)
            MessageBox.Show("Cập nhật dữ liệu đã xong --- Nạp lại chương trình để hoàn tất", "Looking For Software", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }            
    }
}